//
//  YZPersonalCenterVC.m
//  VET
//
//  Created by Lam BG on 2018/6/25.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPersonalCenterVC.h"
#import "UIScrollView+APParallaxHeader.h"
#import "YZPersonalCenterHeaderView.h"
#import "YZPersonalInfoVC.h"
#import "YZProfessionalInfoVC.h"
#import "YZAuthenticationVC.h"
#import "YZCardVC.h"
#import "YZSettingVC.h"
#import "YZUser.h"
#import "YZAuthenticationAuditVC.h"
#import "YZPersonalVM.h"
#import "TBCityIconFont.h"

@interface YZPersonalCenterVC ()
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIView *infoIndicatorView;
@property (weak, nonatomic) IBOutlet UILabel *authStatusLabel;
@property (weak, nonatomic) IBOutlet UIView *authIndicatorView;
@property (strong, nonatomic) YZPersonalVM *viewModel;
@property (weak, nonatomic) YZPersonalCenterHeaderView *headerView;
@property (assign, nonatomic) BOOL needUpdateUserInfo;

// icon
@property (weak, nonatomic) IBOutlet UIImageView *iconJob;
@property (weak, nonatomic) IBOutlet UIImageView *iconLevel;
@property (weak, nonatomic) IBOutlet UIImageView *iconCard;
@property (weak, nonatomic) IBOutlet UIImageView *iconShare;
@property (weak, nonatomic) IBOutlet UIImageView *iconSetting;

// arrow
@property (weak, nonatomic) IBOutlet UIImageView *arrowJob;
@property (weak, nonatomic) IBOutlet UIImageView *arrowLevel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowCard;
@property (weak, nonatomic) IBOutlet UIImageView *arrorShare;
@property (weak, nonatomic) IBOutlet UIImageView *arrowSetting;



@end

@implementation YZPersonalCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableHeaderView];
    [self setupUserInfo];
    // icon
    self.iconJob.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63f", 24, kColorWithHex(0x434343))];
    self.iconLevel.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue633", 24, kColorWithHex(0x434343))];
    self.iconCard.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue628", 24, kColorWithHex(0x434343))];
    self.iconShare.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue634", 24, kColorWithHex(0x434343))];
    self.iconSetting.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue62d", 24, kColorWithHex(0x434343))];
    
    // arrow
    self.arrowJob.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63c", 24, kColorWithHex(0x9B9B9B))];
    self.arrowLevel.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63c", 24, kColorWithHex(0x9B9B9B))];
    self.arrowCard.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63c", 24, kColorWithHex(0x9B9B9B))];
    self.arrorShare.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63c", 24, kColorWithHex(0x9B9B9B))];
    self.arrowSetting.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63c", 24, kColorWithHex(0x9B9B9B))];
    
    // 左边返回按键颜色
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    // 设置导航条的色调 理解为"混合色"
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    // 导航栏默认是半透明状态
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    // 导航透明效果 YES 默认，NO 取笑
    self.navigationController.navigationBar.translucent = NO;
    // 导航栏标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    // 设置backBarButtonItem即可
    self.navigationItem.backBarButtonItem = backItem;
    [self updateUserInfo];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if (self.needUpdateUserInfo) {
        [self updateUserInfo];
        self.needUpdateUserInfo = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)updateUserInfo{
//    @weakify(self)
//    [[self.viewModel requestPersonalBaseInfo] subscribeNext:^(id  _Nullable x) {
//        ;
//    } completed:^{
//        @strongify(self)
//        [self setupUserInfo];
//    }];
    [self setupUserInfo];
}
- (void)setupTableHeaderView{
    YZPersonalCenterHeaderView *headerView = [[YZPersonalCenterHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 193)];
    @weakify(self)
    headerView.personalInfoClickedBlock = ^{
        @strongify(self)
        YZPersonalInfoVC *personalInfoVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZPersonalInfoVC"];
        @weakify(self)
        personalInfoVC.personalInfoUpdateBlock = ^{
            @strongify(self);
            self.needUpdateUserInfo = YES;
        };
        [self.navigationController pushViewController:personalInfoVC animated:YES];
    };
    [self.tableView addParallaxWithView:headerView andHeight:193 andShadow:NO];
    self.headerView = headerView;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.tableFooterView = [UIView new];
}

- (void)setupUserInfo{
    YZUser *loginUser =  [YZUser sharedInstance];
     @weakify(self)
    [[RACObserve(loginUser, nickName) distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
       [self.headerView refreshWithLoginUser:loginUser];
    }];
    [[RACObserve(loginUser, professionType) distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        self.infoLabel.text = loginUser.professionType?:@"未登记";
        self.infoIndicatorView.hidden = loginUser.professionType;
    }];
    [[RACObserve(loginUser, authStatus) distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        if (loginUser.authStatus == 1) {
            self.authStatusLabel.text = @"认证中";
            self.authIndicatorView.hidden = YES;
        }else if (loginUser.authStatus == 2){
            self.authStatusLabel.text = @"已认证";
            self.authIndicatorView.hidden = YES;
        }else{
            self.authStatusLabel.text = @"未认证";
        }
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{//职业信息
            YZProfessionalInfoVC *professionalInfoVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZProfessionalInfoVC"];
            [self.navigationController pushViewController:professionalInfoVC animated:YES];
        }break;
        case 1:{//专业认证
            if ([YZUser sharedInstance].authStatus) {
                YZAuthenticationAuditVC *auditVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZAuthenticationAuditVC"];
                [self.navigationController pushViewController:auditVC animated:YES];
            }else{
                YZAuthenticationVC *authenticationVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZAuthenticationVC"];
                [self.navigationController pushViewController:authenticationVC animated:YES];
            }
            
        }break;
        case 2:{//名片
            YZCardVC *cardVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZCardVC"];
            [self.navigationController pushViewController:cardVC animated:YES];
        }break;
        case 3://邀请
            ;
            break;
        case 4:{//设置
            YZSettingVC *settingVC =[[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZSettingVC"];
            [self.navigationController pushViewController:settingVC animated:YES];
        }break;
        default:
            break;
    }
}

- (YZPersonalVM *)viewModel{
    if (!_viewModel) {
        _viewModel = [[YZPersonalVM alloc] init];
    }
    return _viewModel;
}

@end
